package org.dromara.system.controller.talent;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.CertificateConditionConfigBo;
import org.dromara.system.domain.vo.CertificateConditionConfigVo;
import org.dromara.system.service.ICertificateConditionConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 人才认定条件配置
 *
 * @author 毛宁川
 * @date 2025-07-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/talent/ConditionConfig")
public class CertificateConditionConfigController extends BaseController {

    private final ICertificateConditionConfigService certificateConditionConfigService;

    /**
     * 查询人才认定条件配置列表
     */
    @SaCheckPermission("talent:ConditionConfig:list")
    @GetMapping("/list")
    public TableDataInfo<CertificateConditionConfigVo> list(CertificateConditionConfigBo bo, PageQuery pageQuery) {
        return certificateConditionConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人才认定条件配置列表
     */
    @SaCheckPermission("talent:ConditionConfig:export")
    @Log(title = "人才认定条件配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CertificateConditionConfigBo bo, HttpServletResponse response) {
        List<CertificateConditionConfigVo> list = certificateConditionConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "人才认定条件配置", CertificateConditionConfigVo.class, response);
    }

    /**
     * 获取人才认定条件配置详细信息
     *
     * @param configId 主键
     */
    @SaCheckPermission("talent:ConditionConfig:query")
    @GetMapping("/{configId}")
    public R<CertificateConditionConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                                   @PathVariable Long configId) {
        return R.ok(certificateConditionConfigService.queryById(configId));
    }

    /**
     * 新增人才认定条件配置
     */
    @SaCheckPermission("talent:ConditionConfig:add")
    @Log(title = "人才认定条件配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CertificateConditionConfigBo bo) {
        return toAjax(certificateConditionConfigService.insertByBo(bo));
    }

    /**
     * 修改人才认定条件配置
     */
    @SaCheckPermission("talent:ConditionConfig:edit")
    @Log(title = "人才认定条件配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CertificateConditionConfigBo bo) {
        return toAjax(certificateConditionConfigService.updateByBo(bo));
    }

    /**
     * 删除人才认定条件配置
     *
     * @param configIds 主键串
     */
    @SaCheckPermission("talent:ConditionConfig:remove")
    @Log(title = "人才认定条件配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] configIds) {
        return toAjax(certificateConditionConfigService.deleteWithValidByIds(List.of(configIds), true));
    }

    /**
     * 查询人才认定条件配置列表
     */
    @GetMapping("/selectoptions/{talentLevel}")
    public R<List<CertificateConditionConfigVo>> selectOptions(@PathVariable String talentLevel) {
        List<CertificateConditionConfigVo> resultList= certificateConditionConfigService.selectOptions(talentLevel);
        return R.ok(resultList);
    }

}
